﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Openfree.Framework.DataAccess;
public partial class StockInManager : PMSBase
{
    private SqlCodeConfig codeConfig;
    string cmdText = "SELECT T.*,A.CodeName AS StockInTypeName" + PMSAccess.AppendSql_UserOrg + "  FROM {0} T  LEFT JOIN " + SqlCodeConfig.TableName + " A ON A.CodeValue=T.StockInType AND A.Category='" + PMSCodeCategory.PMS_RKLX + "' where 1=1 ";
    protected void Page_Load(object sender, EventArgs e)
    {
        this.TableName = "IC_StockIn";
        this.Key = "StockInID";
        if (!IsPostBack)
        {
            pnlGrid.Visible = true;
            pnlEdit.Visible = false;
            string sql = string.Format(cmdText + " ORDER BY T.CREATETIME DESC", this.TableName);
            FillGirdView(0, sql);
            InitLoadData();
            if (!string.IsNullOrEmpty(Request["StockInID"]))
            {
                this.btnView_Click(null, e);
            }
        }
        else
        {
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
        }
    }
    private void InitLoadData()
    {
        codeConfig = new SqlCodeConfig(this.Connection);
        DataSet ds = codeConfig.LoadDataByCategory(PMSCodeCategory.PMS_RKLX);
        this.BindDropDownList(S_StockInType, ds, true);
        //this.BindDropDownList(P_StockInType, ds, true);
        codeConfig = new SqlCodeConfig(this.Connection);
        ds = codeConfig.LoadDataByCategory(PMSCodeCategory.PMS_CGLX);
        //this.BindDropDownList(S_PurchaseType, ds, true);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        StringBuilder sql = new StringBuilder(string.Format(cmdText, this.TableName));
        if (!string.IsNullOrEmpty(S_StockInCode.Text))
        {
            sql.Append(" AND T.StockInCode like '%").Append(S_StockInCode.Text).Append("%'");
        }
        if (!string.IsNullOrEmpty(S_StockInDate.Text))
        {
            sql.Append(" AND T.StockInDate = '").Append(S_StockInDate.Text).Append("'");
        }
        if (!string.IsNullOrEmpty(S_StockInType.SelectedValue))
        {
            sql.Append(" AND T.StockInType = '").Append(S_StockInType.SelectedValue).Append("'");
        }
        sql.Append(" ORDER BY T.CREATETIME DESC");
        FillGirdView(0, sql.ToString());
    }
    protected void FillGirdView(int currentPage, string cmdText)
    {
        if (string.IsNullOrEmpty(cmdText))
        {
            cmdText = ViewState[this.ClientID + "cmdText"].ToString();
        }
        SqlSplitPage splitPage = new SqlSplitPage(base.Connection, cmdText);
        splitPage.PageSize = 10;
        DataSet ds = splitPage.LoadDataSet(currentPage);
        //成功查询出数据后缓存
        ViewState[this.ClientID + "cmdText"] = cmdText;
        gridView.ShowFooter = true;
        gridView.NavigationData = splitPage.GetNavigationData(currentPage);
        gridView.DataSource = ds.Tables[0];
        gridView.DataBind();
    }

    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((HyperLink)e.Row.Cells[1].Controls[0]).NavigateUrl = "StockInManager.aspx?StockInID=" + ((HiddenField)e.Row.Cells[0].Controls[1]).Value;
            e.Row.Cells[2].Text = this.ParseDateToShort(e.Row.Cells[2].Text);
            e.Row.Cells[7].Text = this.ParseDateToShort(e.Row.Cells[7].Text);
            e.Row.Cells[9].Text = StockInStatus.GetName(e.Row.Cells[9].Text);
        }
    }

    protected void gridView_SplitPageIndexChange(object sender, CommandEventArgs e)
    {
        FillGirdView(Int32.Parse(e.CommandArgument.ToString()), ViewState[this.ClientID + "cmdText"].ToString());
    }

    /// <summary>
    /// 清空控件，清空指定的TextBox、Label
    /// </summary>
    private void ResetComponent()
    {
        StockIn_Purchase.Visible = false;
        StockIn_Receive.Visible = false;
        StockIn_Borrow.Visible = false;
        StockIn_Other.Visible = false;
        //hfStockInID.Value = string.Empty;
        //this.ClearPrexControlValues(this.DefaultPrex);
        //P_LoginName.Text = SessionManager.Username;
        //P_Department.Text = SessionManager.OrgName;
    }


    protected void btnRemove_Click(object sender, EventArgs e)
    {
        string stockid = Request.Form["StockInID"];
        if (string.IsNullOrEmpty(stockid))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }
        DataSet ds = PMSAccess.GetDataSet(this.TableName, this.Key, stockid);
        if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
        {
            Alert("您选择的记录已经被删除，请刷新数据！");
            return;
        }
        DataRow dr = ds.Tables[this.TableName].Rows[0];
        if (Convert.ToInt32(dr["Status"]) == StockInStatus.IsStockIn)
        {
            Alert("您选择的入库单已经入库完成，不能再进行操作！");
            return;
        }
        base.WriteLog("入库单", "删除入库单'" + stockid + "'!",
            LogType.Warning, this.GetType().ToString());
        PMSAccess.RemoveDataRow(this.TableName, this.Key, stockid);
        PMSAccess.RemoveDataRow("IC_StockInItem", this.Key, stockid);
        FillGirdView(this.gridView.SplitPageIndex, string.Empty);
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
       
    }

    protected void btnView_Click(object sender, EventArgs e)
    {
        string stockid = Request.Form["StockInID"];
        if(sender==null)
            stockid = Request["StockInID"];
        if (string.IsNullOrEmpty(stockid))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }
        DataSet ds = PMSAccess.GetDataSet(this.TableName, this.Key, stockid);
        if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
        {
            Alert("您选择的记录已经被删除，请刷新数据！");
            return;
        }
        DataRow dr = ds.Tables[this.TableName].Rows[0]; 
        string type = dr["StockInType"].ToString();
    
        ResetComponent();
        pnlGrid.Visible = false;
        pnlEdit.Visible = true;
        if (PMSCodeCategory.PMS_RKLX_CaiGou == type)
        { 
            StockIn_Purchase.Visible = true;
            StockIn_Purchase.ViewStockIn_Purchase(stockid); 
        }
        else if (PMSCodeCategory.PMS_RKLX_JieHuan == type)
        {
            StockIn_Borrow.Visible = true;
            StockIn_Borrow.ViewStockIn_Borrow(stockid); 
        }
        else if (PMSCodeCategory.PMS_RKLX_LingLiao == type)
        {
            StockIn_Receive.Visible = true;
            StockIn_Receive.ViewStockIn_Receive(stockid); 

        }
        else if (PMSCodeCategory.PMS_RKLX_QiTa == type)
        {
            StockIn_Other.Visible = true;
            StockIn_Other.ViewStockIn_Other(stockid);
        }

    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnlGrid.Visible = true;
        pnlEdit.Visible = false;
        FillGirdView(this.gridView.SplitPageIndex, string.Empty);
        SessionManager.DataSet = null;
    } 
    public void btnSelect_Click(object sender, EventArgs e)
    { 
    }
    #region  采购入库
    public void btnOrderStockIn_Click(object sender, EventArgs e)
    {
        //hfAddOrEdit.Value = "StockIn_Purchase";
        StringBuilder sql = new StringBuilder(string.Format("SELECT count(*) as dcount FROM {0} T ", "IC_PurchaseOrder"));
        sql.Append(" WHERE 1=1 and T.Status='" + OrderStatus.WaitProcess + "' ");
        DataSet ds = PMSAccess.GetDataSet(this.Connection,sql.ToString());
        if (Convert.ToInt32(ds.Tables[0].Rows[0]["dcount"]) > 0)
        {
            ResetComponent();
            pnlGrid.Visible = false;
            pnlEdit.Visible = true;
            StockIn_Purchase.Visible = true; 
            StockIn_Purchase.AddStockIn_Purchase(); 
        }
        else
        {
            Alert("没有可以选择入库的采购单！");
            return;
        }
    }
    #endregion

    #region  领料入库

    protected void btnBackStockIn_Click(object sender, EventArgs e)
    {
        //hfAddOrEdit.Value = "StockIn_Receive"; 

        ResetComponent();
        pnlGrid.Visible = false;
        pnlEdit.Visible = true;
        StockIn_Receive.Visible = true;
        StockIn_Receive.AddStockIn_Receive();
        return;

        StringBuilder sql = new StringBuilder(string.Format("SELECT count(*) as dcount from IC_StockOut where RequisitionID in (select RequisitionID FROM {0} T WHERE 1=1 and T.Status='" + StockInStatus.UnStockIn + "' )", "IC_Requisition"));
        
        DataSet ds = PMSAccess.GetDataSet(this.Connection, sql.ToString());
        if (Convert.ToInt32(ds.Tables[0].Rows[0]["dcount"]) > 0)
        {
            ResetComponent();
            pnlGrid.Visible = false;
            pnlEdit.Visible = true;
            StockIn_Receive.Visible = true;
            StockIn_Receive.AddStockIn_Receive();
        }
        else
        {
            Alert("没有可以选择任务单的领料单！");
            return;
        }
    }
    #endregion

    #region  借还入库
    protected void btnBorrowStockIn_Click(object sender, EventArgs e)
    {
        //hfAddOrEdit.Value = "StockIn_Borrow";
        ResetComponent();
        pnlGrid.Visible = false;
        pnlEdit.Visible = true;
        StockIn_Borrow.Visible = true;
        StockIn_Borrow.AddStockIn_Borrow();
    }
    #endregion

    #region  其它入库
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //hfAddOrEdit.Value = "StockIn_Other";
        ResetComponent();
        pnlGrid.Visible = false;
        pnlEdit.Visible = true;
        StockIn_Other.Visible = true;
        StockIn_Other.AddStockIn_Other();
    }
    #endregion 
}